package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.DynamicScheduledTaskService;
import com.example.entity.dto.weixiubaoyang.GuZhangTask;
import com.example.entity.yifei.*;
import com.example.enums.ZnxStatusEnum;
import com.example.mapper.*;
import com.example.service.GuzhanglogService;
import com.example.service.ZnxService;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lan
 * @since 2024-11-25
 */
@Service
public class GuzhanglogServiceImpl extends ServiceImpl<GuzhanglogMapper, Guzhanglog> implements GuzhanglogService {
    @Autowired
    private GuzhanglogMapper guzhanglogMapper;
    @Autowired
    private CallCenterConfigMapper callCenterConfigMapper;
    @Autowired
    private CallCenterLeaderMapper callCenterLeaderMapper;
    @Autowired
    private GuzhanghujiaologMapper guzhanghujiaologMapper;
    @Autowired
    private DscmaMapper dscmaMapper;
    @Autowired
    private ZnxService znxService;

    /**
     *
     * @param id guzhanglog(故障单ID)
     * @param level 这次要通知到哪个level的人
     */
    @Override
    @DSTransactional
    public void tongZhiNextPeople(Integer id, String level) {
        if(id == null || id == 0 || StringUtils.isEmpty(level)) return;
        // 1.看一下故障单的状态。如果已经处理了，或者结束了。continue
        Guzhanglog guzhanglog = guzhanglogMapper.selectOne(new LambdaQueryWrapper<Guzhanglog>()
                .eq(Guzhanglog::getId, id));
        if(guzhanglog == null || !"N".equals(guzhanglog.getIsFinish()) || guzhanglog.getCallCenterConfigurationUuid() == null) return;
        // 2.通知到设置表中的人。 如果level不存在，或者故障单中绑定的 配置uuid不存在   continue
        CallCenterConfiguration centerConfigVo = callCenterConfigMapper
                .selectOne(new LambdaQueryWrapper<CallCenterConfiguration>().eq(CallCenterConfiguration::getUuid, guzhanglog.getCallCenterConfigurationUuid()));
        if(centerConfigVo == null) return;
        CallCenterWorkLeader callCenterWorkLeader = callCenterLeaderMapper
                .selectOne(new LambdaQueryWrapper<CallCenterWorkLeader>().eq(CallCenterWorkLeader::getParentId, centerConfigVo.getUuid())
                .eq(CallCenterWorkLeader::getLv, level));
        if(callCenterWorkLeader == null || StringUtils.isEmpty(callCenterWorkLeader.getWorkLeader())) return;
        // 3.通知这一级的所有人。然后添加下一级别的时间。
        String[] split = callCenterWorkLeader.getWorkLeader().trim().split(",");
        if(split.length == 0) return;
        for(int i = 0; i < split.length; i++) {
            // 3.1 插入到表中
            Guzhanghujiaolog hujiao = new Guzhanghujiaolog();
            hujiao.setGuzhanglogid(id);
            hujiao.setLevel(level);
            hujiao.setStatus("N");
            hujiao.setPersonno(split[i]);
            Dscma dscma = dscmaMapper.selectOne(new LambdaQueryWrapper<Dscma>().eq(Dscma::getMa001, split[i]));
            hujiao.setPersonname(dscma.getMa002().trim());
            hujiao.setCreatetime(TimeUtils.getFullNowTime());
            guzhanghujiaologMapper.insert(hujiao);
            // 3.2 消息通知
            znxService.saveZnxAndSendMessageBuZouPeiZhi(split[i],
                    guzhanglog.getMacno()
                            + "上有一个故障类型为："
                            + guzhanglog.getGuZhangTypeName()
                            + "的故障需要处理。",
                    ZnxStatusEnum.DAIBAN.getValue(), "");
        }
        // 4.如果有下一级的话，添加到Map中。否则结束
        if(StringUtils.isEmpty(callCenterWorkLeader.getResponseTime())) return;  // 这一级没有响应时间的话，直接结束
        int i = Integer.parseInt(level);
        i++;
        CallCenterWorkLeader workLeader = callCenterLeaderMapper.
                getNextLvByParentIdAndLv
                        (guzhanglog.getCallCenterConfigurationUuid(), i+"");
        if(workLeader == null) return;
        int timeInt = Integer.parseInt(callCenterWorkLeader.getResponseTime()); // 从现在开始往后多长时间
        Map<String, GuZhangTask> scheduledTasks = DynamicScheduledTaskService.scheduledTasks;
        GuZhangTask task = new GuZhangTask();
        String uuid = UUID.randomUUID().toString();
//        task.setIdid(uuid); // UUID
        task.setLevel(workLeader.getLv());  // 下一个级别
        task.setTimes(LocalDateTime.now().plusMinutes(timeInt)); // 下一次要执行的时间
        task.setGuzhangdanId(id.toString());  // 故障单ID
        scheduledTasks.put(uuid, task);
    }

}
